package mil.af.amc.adpetracker.dao.hibernate;

import mil.af.amc.adpetracker.dao.OrganizationDao;
import mil.af.amc.adpetracker.model.Organization;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Service;

import java.sql.SQLException;

/**
 * Please Fill in this Javadoc
 *
 * @author TSgt Jason Ferguson
 */
@Service("organizationDao")
public class HibernateOrganizationDao extends HibernateGenericDao<Organization> implements OrganizationDao {

    public HibernateOrganizationDao() {
        super(Organization.class);
    }

    public Organization getByOrganizationName(final String name) {

        return (Organization) getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException, SQLException {

                Criteria crit = session.createCriteria(Organization.class);
                crit.add(Restrictions.eq("name", name.toUpperCase()));
                return crit.uniqueResult();
            }
        });

    }
}
